package com.k.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.RedBag;
import com.k.demo.entity.RedBagRecord;
import com.k.demo.entity.Vips;
import com.k.demo.service.CustomerUserService;
import com.k.demo.service.RedBagRecordService;
import com.k.demo.service.RedBagService;
import com.k.demo.service.VipService;

@CrossOrigin
@RestController
@RequestMapping("/vip")
public class VipController {
	@Autowired
	private VipService vipService;
	@Autowired
	private CustomerUserService customeruserService;
	@Autowired
	private RedBagRecordService redBagRecordService;
	@Autowired
	private RedBagService redBagService;
	
	@RequestMapping("/all")
	public Object getAll() {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("vipList",vipService.getAll());
		
		return map;
	}
	
	@RequestMapping("/all/page")
	public Object getAllByPage(@RequestParam int pageNum,@RequestParam int pageSize) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<Vips> viplist = vipService.getAll();
		
		PageInfo pageinfo =new PageInfo<>(viplist,pageSize);
		
		map.put("pageinfo", pageinfo);
		
		return map;
	}
	
	@RequestMapping("/getbylv")
	public Object getByLv(@RequestParam int vip_lv,@RequestParam int pageNum,@RequestParam int pageSize) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		PageHelper.startPage(pageNum, pageSize);
		
		List<Vips> viplist = vipService.getByLv(vip_lv);
		
		PageInfo pageinfo =new PageInfo<>(viplist,pageSize);
		
		map.put("pageinfo", pageinfo);
		
		return map;
	}
	
	@RequestMapping("/getbyuserid")
	public Object getById(@RequestParam int user_id) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("vip", vipService.getByUserId(user_id));
		
		return map;
	}
	
	@RequestMapping("/getcurrentvip")
	public Object getCurrentVip(HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		
		map.put("customeruser", customerUser);
		
		map.put("vip", vipService.getByUserId(customerUser.getUser_id()));
		
		return map;
	}
	
	@RequestMapping("/getbyusernameandlv/page")
	public Object getByUserNameAndLv(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		List<Integer> user_id = customeruserService.getIdByName((String)map.get("user_true_name"));
		List<Vips> viplist= new ArrayList<Vips>();
		Vips temp =new Vips();
		temp.setVip_lv((Integer)map.get("vip_lv"));
		if(user_id.size()==0) {
			if(map.get("user_true_name")!=null && map.get("user_true_name")!="") {
				rmap.put("err", "没有此用户");
				return rmap;
			}
			
			PageHelper.startPage((int)map.get("pageNum"), (int)map.get("pageSize"));
			PageInfo pageinfo =new PageInfo<>(vipService.getByUserIdAndLv(temp),(int)map.get("pageSize"));
			rmap.put("vip",pageinfo);
			
			return rmap;
			
		}else {
			for(int i : user_id) {
				temp.setUser_id(i);
				for(Vips v :vipService.getByUserIdAndLv(temp))
				viplist.add(v);
			}
		}
		
		PageHelper.startPage((int)map.get("pageNum"), (int)map.get("pageSize"));
		PageInfo pageinfo =new PageInfo<>(viplist,(int)map.get("pageSize"));
		rmap.put("vip",pageinfo);
		
		return rmap;
	}
	@RequestMapping("/add")
	public Object addVip(@RequestBody Vips vip) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(vipService.getByUserId(vip.getUser_id())!=null) {
		
			map.put("insertnum", vipService.addVip(vip));
		}else {
			map.put("err", "这个用户已经是vip了");
		}	
		
		return map;
	}
	
	@RequestMapping("/delete")//注意这里是user_id数组
	public Object deleteForEach(@RequestBody List<Integer> deletelist) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("deletenum", vipService.deleteVip(deletelist));
		
		return map;
	}
	
	@RequestMapping("/update")
	public Object updateVip(@RequestBody Vips vip) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("updatenum", vipService.updateVip(vip));
		
		return map;
	}
	
	//充值成为vip
	@RequestMapping("/becomevip")
	public Object becomeVio(@RequestParam Integer vip_lv,@RequestParam Long time,HttpSession session) throws ParseException{

		
		System.out.println("准备变成vip拉");
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//获取当前用户
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		//获取userid
		int userid = customerUser.getUser_id();
		
		//获取当前用户的vip信息
		
		//当前用户为新注册会员，此时会给他发红包
		if(vipService.getByUserId(userid)== null ) {

			//获取当前系统时间并与传来的时长进行计算
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
		    String nowtime = df.format(date);
		    //获取创建时间用于计算
		    Date create_time = df.parse(nowtime);
		    //接受的参数时微秒级别 (月 天 小时 分钟  秒)
		    Date newTime = new Date(date.getTime() + time * 30 * 24 * 60 * 60 * 1000);
		    //结束时间格式化
			String end_time = df.format(newTime);
			
			//创建vip对象
			Vips vip = new Vips();
			vip.setVip_lv(vip_lv);
			vip.setUser_id(userid);
			vip.setStart_date(nowtime);
			vip.setEnd_date(end_time);
			//获取当前可领取的红包列表
			List<RedBag> vipRedBags = redBagService.getVipRedBags();
			//更新用户的可用红包列表(即向红包记录表中添加数据)
			for(RedBag red  : vipRedBags) {
				RedBagRecord redBagRecord = new RedBagRecord();
				redBagRecord.setUser_id(customerUser.getUser_id());
				redBagRecord.setRed_bag_id(red.getRed_bag_id());
				redBagRecord.setUser_true_name(customerUser.getUser_true_name());
				redBagRecord.setCreate_date(nowtime);
				redBagRecord.setValid_time(30);
				redBagRecord.setRed_bag_state(61);
				//添加到领取记录
				redBagRecordService.addRedBagRecord(redBagRecord);
			}
			map.put("addResult", vipService.addVip(vip));
			map.put("msgFlag", true);
			
			return map;
			
		}else {
		//根据当前用户等级判断
			Vips nowVips = vipService.getByUserId(userid);
			//如果等级相同就是续费
			if(nowVips.getVip_lv() == vip_lv) {
				//更新vip的到期时间
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Vips vips = new Vips();
				Date oldEndDate = df.parse(nowVips.getEnd_date());
				//这边 根据当前时间进行续费
				String newEndDate = df.format(new Date(oldEndDate.getTime() + time * 30 * 24 * 60 * 60 *1000));
				vips.setEnd_date(newEndDate);
				vips.setUser_id(userid);
				//返回跟新结果
				map.put("Msg", "续费成功");
				map.put("payResult", vipService.updateVip(vips));
				
				return map;
			}else {
				//等级不同就是升级
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				//获取当前时间
				Date now = new Date();
				//更新时间
				String newEndDate = df.format(new Date(now.getTime() + time * 30 * 24 * 60 * 60 *1000));
				Vips vips = new Vips();
				vips.setVip_lv(vip_lv);
				vips.setUser_id(userid);
				vips.setEnd_date(newEndDate);
				map.put("Msg", "升级成功");
				map.put("payResult", vipService.updateVip(vips));
				return map;
				
			}
		
		}
		
		
	}

	
}
